package com.vcaresolution.logistic.service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.vcaresolution.logistic.form.ShippingbillForm;
import com.vcaresolution.logistic.pojo.Codetable;
import com.vcaresolution.logistic.pojo.Jobregister;
import com.vcaresolution.logistic.pojo.Shippingbill;
import com.vcaresolution.logistic.to.ShippingbillTO;
import com.vcaresolution.util.Utils;

@Service
public class ShippingbillServiceImpl extends BaseService implements ShippingbillService{

	private Shippingbill populateShippingbillPojo(ShippingbillForm shippingbillForm){
		Shippingbill shippingbill = new Shippingbill();
		BeanUtils.copyProperties(shippingbillForm, shippingbill);
		
		return shippingbill;
	}
	
	@Override
	@Transactional
	public void add(ShippingbillForm shippingbillForm) {
		Shippingbill shippingbill = populateShippingbillPojo(shippingbillForm);
		
		shippingbill.setJobregister(getDaoFactory().getUtilityDAO().findById(Jobregister.class, shippingbillForm.getJobnoid()));
		shippingbill.setCodetableByDirecthubid(getDaoFactory().getCodetableDAO().findById(Codetable.class, shippingbillForm.getDirecthubid()));
		shippingbill.setCodetableByIncotermsid(getDaoFactory().getCodetableDAO().findById(Codetable.class, shippingbillForm.getIncotermsid()));
		shippingbill.setCodetableByCurrencyid(getDaoFactory().getCodetableDAO().findById(Codetable.class, shippingbillForm.getCurrencyid()));
		
		shippingbill.setCreationdate(Utils.getCurrentDate());
						
		getDaoFactory().getUtilityDAO().add(shippingbill);
	}

	@Override
	@Transactional
	public void update(ShippingbillForm shippingbillForm) {
		Shippingbill shippingbill = populateShippingbillPojo(shippingbillForm);
		
		shippingbill.setJobregister(getDaoFactory().getUtilityDAO().findById(Jobregister.class, shippingbillForm.getJobnoid()));
		shippingbill.setCodetableByDirecthubid(getDaoFactory().getCodetableDAO().findById(Codetable.class, shippingbillForm.getDirecthubid()));
		shippingbill.setCodetableByIncotermsid(getDaoFactory().getCodetableDAO().findById(Codetable.class, shippingbillForm.getIncotermsid()));
		shippingbill.setCodetableByCurrencyid(getDaoFactory().getCodetableDAO().findById(Codetable.class, shippingbillForm.getCurrencyid()));
		
		getDaoFactory().getUtilityDAO().update(shippingbill);
	}

	@Override
	@Transactional
	public void delete(ShippingbillForm shippingbillForm) {
		getDaoFactory().getUtilityDAO().delete(populateShippingbillPojo(shippingbillForm));
	}

	@Override
	@Transactional
	public ShippingbillTO findById(Serializable sbID) {
		Shippingbill shippingbill = (Shippingbill) getDaoFactory().getUtilityDAO().findById(Shippingbill.class,sbID);
		
		ShippingbillTO shippingBillTO = new ShippingbillTO();
		BeanUtils.copyProperties(shippingbill, shippingBillTO);
		
		shippingBillTO.setDirecthubid(shippingbill.getCodetableByDirecthubid().getCodetableid());
		shippingBillTO.setIncotermsid(shippingbill.getCodetableByIncotermsid().getCodetableid());
		
		shippingBillTO.setCurrency(shippingbill.getCodetableByCurrencyid().getFormvalue());
		shippingBillTO.setCurrencyid(shippingbill.getCodetableByCurrencyid().getCodetableid());
		
		shippingBillTO.setJobnoid(shippingbill.getJobregister().getJobnoid());
		
		return shippingBillTO; 
	}

	@Override
	@Transactional
	public List<ShippingbillTO> findByAll() {
		List<Shippingbill> shippingbillList = getDaoFactory().getUtilityDAO().findByAll(Shippingbill.class);
		
		List<ShippingbillTO> shippingbillFormList = new ArrayList<ShippingbillTO>();
		
		if( shippingbillList != null && !shippingbillList.isEmpty() ){
			ShippingbillTO shippingBillTO = null;
			for(Shippingbill shippingbill: shippingbillList){
				shippingBillTO = new ShippingbillTO();
				
				shippingBillTO.setCurrency(shippingbill.getCodetableByCurrencyid().getFormvalue());
				
				BeanUtils.copyProperties(shippingbill, shippingBillTO);
				shippingbillFormList.add(shippingBillTO);
			}
		}	
		return shippingbillFormList;
	}

	@Override
	@Transactional
	public List<ShippingbillTO> findByAll(Serializable jobId) {
		List<Shippingbill> shippingbillList = getDaoFactory().getUtilityDAO().findByCondition(Shippingbill.class, Utils.getEqExpressionList("jobregister.jobnoid", jobId));
		
		List<ShippingbillTO> shippingbillFormList = new ArrayList<ShippingbillTO>();
		
		if( shippingbillList != null && !shippingbillList.isEmpty() ){
			ShippingbillTO shippingBillTO = null;
			for(Shippingbill shippingbill: shippingbillList){
				shippingBillTO = new ShippingbillTO();
				
				shippingBillTO.setCurrency(shippingbill.getCodetableByCurrencyid().getFormvalue());
				
				BeanUtils.copyProperties(shippingbill, shippingBillTO);
				shippingbillFormList.add(shippingBillTO);
			}
		}	
		return shippingbillFormList;
	}
}